function hide_element(id)
{
	$("#"+id).css("display", "none");
}

function show_element(id, type)
{
	$("#"+id).css("display", type);
}

function hide_comment_form()
{
	$("#comment_form").css("display", "none");
}

/**
 * @param eid id of the element after which comment form has to be shown
 * @return
 */
function show_comment_form(eid, to_comment)
{
	hide_comment_form();
	$("#fe-f285a77b1dbe20195abbed303ba917b9").val(to_comment);
	elem = $(eid);
	form = $("#comment_form");
	elem.append(form);
	form.css("display", "block");
}

/**
 * Show plain login form and hide openid form.
 */
function show_plain_login_form()
{
	// hide openid login form
	hide_element("login-applet-openid");
	// hide switch link
	hide_element("switch-to-plain");
	
	// show plain login form
	show_element("login-applet-plain", "block");
	// show switch link
	show_element("switch-to-openid", "inline");
	
	return false;
}

/**
 * Show openid login form and hide plain.
 */
function show_openid_login_form()
{
	// hide plain login form
	hide_element("login-applet-plain");
	// hide switch link
	hide_element("switch-to-openid");
	
	// show openid login form
	show_element("login-applet-openid", "block");
	// show switch link
	show_element("switch-to-plain", "inline");
	
	return false;
}

/**
 * Submit displayed login form (plain or openid)
 */
function do_login()
{
	if ($("#login-applet-plain").css("display") == "block") {
		// perform plain login
		$("#login-form-plain").submit();
	} else {
		$("#login-form-openid").submit();
	}
}

/**
 * Process onChange event of FileUpload object
 * 
 * @param f id of <input type="file"> element
 * @param t id of target <input type="text"> element
 * @return
 */
function fileupload_process_change(f, t)
{
	f = $("#"+f);
	t = $("#"+t);
	
	var path = f.attr('value');
	// code from mediawiki 
	// Find trailing part
	var slash = path.lastIndexOf('/');
	var backslash = path.lastIndexOf('\\');
	var fname;
	if (slash == -1 && backslash == -1) {
		fname = path;
	} else if (slash > backslash) {
		fname = path.substring(slash+1, 10000);
	} else {
		fname = path.substring(backslash+1, 10000);
	}
	
	/*
	// Capitalise first letter and replace spaces by underscores
	fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
	
	// remove known file extensions
	var known_types = Array("jpg", "png");
	for (i in known_types) {
		ext = known_types[i];
		var re = new RegExp("\."+ext+"$");
		if (null != re.exec(fname.toLowerCase()) ) {
			fname = fname.substring(0, fname.length-ext.length-1);
			break;
		}
	}
	*/
	
	// Output result
	t.attr('value', fname);
}

function show_comment_preview(src)
{
	$("#e-preview_frame").css("display", "block");
	
	// submit form
	var form = $("#e-preview_form");
	var markup_text = $("#fe-markup_text");
	var source = $("#"+src);
	
	markup_text.attr("value", source.attr("value"));
	form.submit();
}

function copy_element_contents(e, target_id)
{
	var target = $("#"+target_id);
	target.val(e.innerHTML);
}
